package com.bee.剑指offer;

public class 二分查找2 {

    public static int search (int[] nums, int target) {
        int low = 0;
        int high = nums.length - 1;

        while (low <= high) {
            int mid = (low + high) / 2;
            if(nums[mid] == target) {
                int temp = mid;
                while (temp >= 0 && nums[temp] == target) {
                    temp--;
                }
                return temp + 1;
            }
            if(target < nums[mid]) {
                high = mid - 1;
            }
            if(target > nums[mid]) {
                low = mid + 1;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        int search = search(new int[]{1,2,2,3,4}, 2);
        System.out.println(search);
    }
}
